//
//  CustomStatusBar.m
//  etionUI
//
//  Created by wu jingxing on 12-8-30.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "CustomStatusBar.h"

@interface CCustomStatusBar () <CStaticTextLabelDelegate>
{
    CStaticTextLabel *_textlabel;
}

@end

@implementation CCustomStatusBar

@synthesize text;

@synthesize textColor;

@synthesize textFont;

@synthesize customView;

@synthesize delegate;

- (id)init
{
    [super initWithFrame:CGRectZero];

    CGRect frame = [UIApplication sharedApplication].statusBarFrame;
    
    if(UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
        frame.size=CGSizeMake(frame.size.height, frame.size.width);

    frame.origin.x = frame.size.width - 120;
    frame.size.width = 120 - 3;
    self.frame = frame;

    self.windowLevel = UIWindowLevelStatusBar + 1;

    self.backgroundColor = [UIColor blackColor];

    self.hidden = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    /*
    if([[UIDevice currentDevice].systemVersion floatValue]>=7)
    {
        CViewBaseViewController *vcl = nil;
        if([vcl isKindOfClass:[CViewBaseViewController class]])
        {
            __block CCustomNavigationBar *bar = [vcl valueForKey:@"_navBar"];
            
            if(bar == nil)
            {
                [vcl enumFindKindOfViewController:[CViewBaseViewController class] findedViewController:^(CViewBaseViewController *findedvcl)
                {
                    if(bar == nil)
                        bar = [findedvcl valueForKey:@"_navBar"];
                }];
            }
            
            if(bar!=nil&&[bar isKindOfClass:[CCustomNavigationBar class]])
            {
                UIImageView *imageview=[bar valueForKey:@"_backgroundImageView"];
                if(imageview!=nil)
                {
                    UIGraphicsBeginImageContext(imageview.size);
                    CGContextRef ctx=UIGraphicsGetCurrentContext();
                    CGContextSaveGState(ctx);
                    [imageview.layer renderInContext:ctx];
                    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
                    CGContextRestoreGState(ctx);
                    UIGraphicsEndPDFContext();
                    self.backgroundColor=[UIColor colorWithPatternImage:image];
                    
                    image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, self.frame)];
                    self.backgroundColor=[UIColor colorWithPatternImage:image];
                }
            }
        }
    }
     */
#endif

    return self;
}

- (id)createTextLabel
{
    CGRect frame = self.bounds;
    //    frame.origin.x+=10;
    //    frame.size.width-=20;
    _textlabel = [[CStaticTextLabel alloc] initWithFrame:frame];
    _textlabel.delegate = self;
    _textlabel.font = [UIFont systemFontOfSize:12];
    _textlabel.textColor = [UIColor whiteColor];
    _textlabel.backgroundColor = [UIColor clearColor];
    _textlabel.textAlignment = NSTextAlignmentRight;
    _textlabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _textlabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [self addSubview:_textlabel];

    NSInteger nVersion = [[UIDevice currentDevice].systemVersion integerValue];
    if (nVersion == 6 || nVersion == 5)
    {
        if ([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleLightContent)
            _textlabel.textColor = [UIColor whiteColor];
    }

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    else if(nVersion>=7)
    {
        if([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleDefault)
            _textlabel.textColor=[UIColor blackColor];
        else if([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleLightContent)
            _textlabel.textColor=[UIColor whiteColor];
    }
#endif

    return self;
}

- (void)setTextColor:(UIColor *)color
{
    if (_textlabel == nil)
        [self createTextLabel];
    _textlabel.textColor = color;
}

- (void)setText:(NSString *)szText
{
    if (_textlabel == nil)
        [self createTextLabel];
    _textlabel.text = szText;
}

- (NSString *)szText
{
    return _textlabel.text;
}

- (void)setText:(NSString *)szText animated:(BOOL)bAnimated
{
}

- (void)setTextFont:(UIFont *)font
{
    if (_textlabel == nil)
        [self createTextLabel];
    _textlabel.font = font;
}

- (UIFont *)textFont
{
    return _textlabel.font;
}

- (void)setCustomView:(UIView *)view
{
    [_textlabel removeFromSuperview];
    ReleaseObj(_textlabel);
    [self addSubview:view];
    view.frame = self.bounds;
}

- (void)staticTextLabelClick:(CStaticTextLabel *)label
{
    if ([self.delegate respondsToSelector:@selector(customStatusBarClick:)])
        [self.delegate customStatusBarClick:self];
}

- (void)dealloc
{
    [_textlabel release_mm];
    superdealloc_mm;
}

@end
